package com.mybatis.dao.impl;

import com.mybatis.dao.IUserDao;
import com.mybatis.domain.User;
import com.mybatis.handler.UserResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements IUserDao {

    private SqlSessionFactory factory;

    public UserDaoImpl(SqlSessionFactory factory){
        this.factory = factory;
    }

    @Override
    public List<User> findAll() {
        //根据factory获取SqlSeesion对象
        SqlSession session = factory.openSession();
        //调用sqlSession的方法，执行sql
        List<User> users = session.selectList("com.mybatis.dao.IUserDao.findAll");
        //释放资源
        session.close();
        return users;
    }

    public void updateUser(User user){
        //根据factory获取SqlSeesion对象
        SqlSession session = factory.openSession();
        //调用sqlSession的方法，执行sql
        session.update("com.mybatis.dao.IUserDao.updateUser", user);
        session.commit();
        //释放资源
        session.close();
    }

    @Override
    public User findUserById(int id) {
        //根据factory获取SqlSeesion对象
        SqlSession session = factory.openSession();
        //调用sqlSession的方法，执行sql
        session.select("com.mybatis.dao.IUserDao.findUserById", id, new UserResultHandler());
        session.commit();
        //释放资源
        session.close();
        return null;
    }


}
